package com.wn.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.wn.pojo.Video;
import com.wn.service.IVideoService;
import com.wn.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-21
 */
@RestController
@RequestMapping("/video")
public class VideoController extends BaseController {

    @Autowired
    private IVideoService videoService;

    //查询
    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result findVideoById(@PathVariable("id") String id) {

        Video video = videoService.getById(id);
        return toDataResult(video);

    }

    //修改、添加
    @ApiOperation("添加、修改操作")
    @PostMapping
    public Result findVideoById(@RequestBody Video video) throws Exception {

        //防止出现脏数据
        if (StringUtils.isEmpty(video.getChapterId())){
            throw new Exception("没有课程章节 抛出异常");
        }

        boolean flag = videoService.saveOrUpdate(video);
        return toResult(flag);

    }

    //删除
    @ApiOperation("添加、修改操作")
    @DeleteMapping ("/{id}")
    public Result deleteVideoById(@PathVariable String id) {

        boolean flag = videoService.removeById(id);
        return toResult(flag);

    }

}
